<?php

namespace app\daren\controller\admin\job;

use app\operations\model\Store;
use sheep\controller\Backend;
use think\exception\ValidateException;
use think\facade\Db;
use think\Request;
use app\daren\model\job\ProbeJob as ProbeJobModel;
use app\daren\validate\admin\job\NewJob as NewJobValidate;

// 实探任务
class ProbeJob extends Backend
{
    public $organ_id;

    public function initialize()
    {
        $this->organ_id = $this->shopId();
        $this->model = new ProbeJobModel;
    }

    /**
     * 商品列表
     *
     * @return \think\Response
     */
    public function list(Request $request)
    {
        $user = $this->auth()->user();
        $store_id = $this->shopId();
        $param = $request->param();
        if ($user->role_id != 1) {
            $param['store_id'] = $store_id;
        }
        $daren = $this->model->getList($param);
        return success('获取成功', $daren);
    }

    // 添加
    public function add(Request $request)
    {
        $param = $request->only([
            'title', 'title_cover_image', 'activity_time', 'end_time', 'start_time', 'take_material', 'platform_require', 'work_form', 'province_limit', 'other_require', 'store_id', 'link', 'mobile', 'poiId', 'selling_point', 'shop_address', 'receiving_level'
        ]);
//        try {
//            validate(NewJobValidate::class)
//                ->scene('add')
//                ->check($param);
//        } catch (ValidateException $e) {
//            // 验证失败 输出错误信息
//            return error($e->getError());
//        }
        $user              = $this->auth()->user();
        if ($user->role_id != 1) {
            $param['organ_id'] = $user->organ_id;
            $param['store_id'] = $this->organ_id;
        } else {
            $store_data = Store::where(['id' => $param['store_id']])->findOrEmpty();
            $param['organ_id'] = $store_data['organ_id'];
        }

        $result = $this->model->dataCreate($param);
        if (!$result) {
            return error('保存失败');
        }

        // 同步萌宠霸王
        $param['receiving_level'] = json_encode($param['receiving_level'], JSON_UNESCAPED_UNICODE);
        $param['activity_time'] = strtotime($param['activity_time']);
        $param['start_time'] = strtotime($param['start_time']);
        $param['end_time'] = strtotime($param['end_time']);
        $param['external_id'] = $result->id;
        $param['create_time']         = time();
        $param['update_time']         = time();
        $cutepetDb                    = Db::connect('mysql_cutepet');
        $cutepetGroupGoods            = $cutepetDb->name('field_probe_job')->insert($param);
        if (!$cutepetGroupGoods) {
            return error('同步萌宠霸王失败');
        }

        return success('保存成功');
    }

    /**
     * 商品详情
     *
     * @return \think\Response
     */
    public function detail(Request $request, $id)
    {
        $goods = $this->model->detail($id);
        return success('获取成功', $goods);
    }

    // 审核
    public function statusEdit(Request $request)
    {
        $param = $request->only([
            'id', 'status', 'remark'
        ]);
        if (empty($param['id']) || empty($param['status'])) {
            return error('参数有误');
        }
        $user = $this->auth()->user();
        $daren = $this->model->detail($param['id']);
        if ($daren->isEmpty()) {
            return error('实探任务不存在');
        }
        if ($daren->status['value'] != 1) {
            return error('该实探任务已审核');
        }

        $result = $this->model->dataStatusEdit($param);
        if (!$result) {
            return error('修改失败');
        }

        // 同步萌宠霸王
        $id = $result->id;
        unset($param['id']);
//        $param['pass_time']  = time();

        $cutepetDb             = Db::connect('mysql_cutepet');
        $cutepetGroupGoods     = $cutepetDb->name('field_probe_job')->where('external_id', $id)->update($param);
        if (!$cutepetGroupGoods) {
            return error('同步萌宠霸王失败');
        }

        return success('审核成功');
    }

    // 上下架
    public function shelfStatusEdit(Request $request)
    {
        $param = $request->only([
            'id', 'shelf_status'
        ]);
        if (empty($param['id']) || empty($param['shelf_status'])) {
            return error('参数有误');
        }
        $user = $this->auth()->user();
        if ($param['shelf_status'] == 1) {
            $param['start_time'] = time();
        }

        $result = $this->model->shelfStatusEdit($param);
        if (!$result) {
            return error('修改失败');
        }

        // 同步萌宠霸王
        $id = $result->id;
        unset($param['id']);
//        $param['pass_time']  = time();

        $cutepetDb             = Db::connect('mysql_cutepet');
        $cutepetGroupGoods     = $cutepetDb->name('field_probe_job')->where('external_id', $id)->update($param);
        if (!$cutepetGroupGoods) {
            return error('同步萌宠霸王失败');
        }

        return success('成功');
    }

    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        if (empty($id))
        {
            return error('参数有误');
        }
        $result = $this->model->deleteData($id);
        if (!$result) {
            return error('删除失败');
        }
        // 同步萌宠霸王
        $param['update_time']  = time();
        $param['delete_time']  = time();

        $cutepetDb             = Db::connect('mysql_cutepet');
        $cutepetGroupGoods     = $cutepetDb->name('field_probe_job')->where('external_id', $id)->update($param);
        if (!$cutepetGroupGoods) {
            return error('同步萌宠霸王失败');
        }

        return success('删除成功');
    }
}